<template>
  <el-card class="box-card" style="margin: 10px 0px">
    <div slot="header" class="clearfix">
      <el-tabs class="tab" v-model="activename">
        <el-tab-pane label="销售额" name="first"></el-tab-pane>
        <el-tab-pane label="访问量" name="second"></el-tab-pane>
      </el-tabs>
      <div class="right">
        <span @click="setday">今日</span>
        <span @click="setweek">本周</span>
        <span @click="setmonth">本月</span>
        <span @click="setyear">本年</span>
        <el-date-picker
          v-model="date"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
          value-format="yyyy-MM-dd"
          size="mini"
        >
        </el-date-picker>
      </div>
    </div>
    <!-- echarts视图区域 -->
    <div>
      <el-row :gutter="10">
        <el-col :span="18">
          <!-- 定义echarts容器 -->
          <div class="echart" ref="echarts"></div>
        </el-col>
        <el-col :span="6">
          <h3>门店{{ title }}排名</h3>
          <ul>
            <li>
              <span class="spanindex">0</span>
              <span style="padding-left: 10px">肯德基</span>
              <span class="spanvalue">123445</span>
            </li>
            <li>
              <span class="spanindex">1</span>
              <span style="padding-left: 10px">肯德基</span>
              <span class="spanvalue">123445</span>
            </li>
            <li>
              <span class="spanindex">2</span>
              <span style="padding-left: 10px">肯德基</span>
              <span class="spanvalue">123445</span>
            </li>
            <li>
              <span class="spanindex">3</span>
              <span style="padding-left: 10px">肯德基</span>
              <span class="spanvalue">123445</span>
            </li>
            <li>
              <span>4</span>
              <span style="padding-left: 10px">肯德基</span>
              <span class="spanvalue">123445</span>
            </li>
            <li>
              <span>5</span>
              <span style="padding-left: 10px">肯德基</span>
              <span class="spanvalue">123445</span>
            </li>
            <li>
              <span>6</span>
              <span style="padding-left: 10px">肯德基</span>
              <span class="spanvalue">123445</span>
            </li>
            <li>
              <span>7</span>
              <span style="padding-left: 10px">肯德基</span>
              <span class="spanvalue">123445</span>
            </li>
          </ul>
        </el-col>
      </el-row>
    </div>
  </el-card>
</template>

<script>
//引入echarts
import * as echarts from "echarts";
//引入dayjs
import dayjs from "dayjs";
import en from 'element-ui/lib/locale/lang/en';
export default {
  name: "",
  data() {
    return {
      activename: "first",
      mycharts: null,
      //收集日期数据的数组
      date: [],
    };
  },
  methods: {
    //点击今日触发的函数
    setday() {
      const day = dayjs().format("YYYY-MM-DD");
      this.date = [day, day];
    },
    //点击本周触发的事件
    setweek(){
      const start=dayjs().day(1).format("YYYY-MM-DD");
      const end=dayjs().day(7).format("YYYY-MM-DD");
      this.date=[start,end]
    },
    //点击本月触发的事件
    setmonth(){
      const start=dayjs().startOf('month').format('YYYY-MM-DD');
      const end=dayjs().endOf('month').format('YYYY-MM-DD');
      this.date=[start,end]
    },
    //点击本年触发的事件
    setyear(){
      const start=dayjs().startOf('year').format('YYYY-MM-DD');
      const end=dayjs().endOf('year').format('YYYY-MM-DD');
      this.date=[start,end]
    }
  },
  mounted() {
    //初始化echarts
    this.mycharts = echarts.init(this.$refs.echarts);
    //配置echarts数据
    //echarts配置数据如果有新的数据可以再次修改
    this.mycharts.setOption({
      title: {
        text: this.title + "趋势",
      },
      tooltip: {
        trigger: "axis",
        axisPointer: {
          type: "shadow",
        },
      },
      grid: {
        left: "3%",
        right: "4%",
        bottom: "3%",
        containLabel: true,
      },
      xAxis: [
        {
          type: "category",
          data: [
            "一月",
            "二月",
            "三月",
            "四月",
            "五月",
            "六月",
            "七月",
            "八月",
            "九月",
            "十月",
            "十一月",
            "十二月",
          ],
          axisTick: {
            alignWithLabel: true,
          },
        },
      ],
      yAxis: [
        {
          type: "value",
        },
      ],
      series: [
        {
          name: "Direct",
          type: "bar",
          barWidth: "60%",
          data: [10, 52, 200, 334, 390, 330, 220, 200, 150, 170, 90, 100],
          color: "yellowgreen",
        },
      ],
    });
  },
  //设置动态显示的门店标题
  computed: {
    title() {
      return this.activename == "first" ? "销售额" : "访问量";
    },
  },
  watch: {
    title() {
      //重新设置柱状图的标题数据
      this.mycharts.setOption({
        title: {
          text: this.title,
        },
      });
    },
  },
};
</script>

<style scoped>
.clearfix {
  position: relative;
  display: flex;
  justify-content: space-between;
}
.tab {
  width: 100%;
}
.right {
  position: absolute;
  right: 0px;
}
.el-range-editor--mini.el-input__inner {
  width: 350px;
  margin: 0 20px;
}
.right span {
  cursor: pointer;
  margin: 0 10px;
}
.echart {
  width: 100%;
  height: 300px;
}
ul {
  list-style: none;
  width: 100%;
  height: 300px;
  padding: 0px;
}
ul li {
  height: 8%;
  margin: 5px;
}
.spanindex {
  float: left;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: black;
  color: white;
  text-align: center;
}
.spanvalue {
  float: right;
}
</style>